package daily;

import java.util.*;

import java.util.stream.Collectors;

/**
 * @author MtRider
 * @version 1.0
 * @description 删除排序数组中的重复项
 * @GiteeAndGithub MtRider
 * @date 2022/4/20 17:03
 */
public class demo01 {
    public static void main(String[] args) {
        int[] nums = {1, 1, 2, 4, 4, 3};
        //System.out.println(removeDuplicates(nums));
        //System.out.println(removeDuplicates2(nums));
        System.out.println(removeDupicates3(nums));
    }

    /*1, 1, 2, 4, 4, 3*/
    public static int removeDuplicates(int[] nums) {
        if (nums.length < -1) {
            return nums.length;
        }
        int j = 0;
        for (int i = 0; i < nums.length; i++) {
            //点睛之比
            if (nums[i] != nums[j]) {
                nums[++j] = nums[i];
            }
        }
        return j + 1;

    }

    //stream流去重
    public static List<Integer> removeDuplicates2(int[] nums) {
        List<Integer> collect = Arrays.stream(nums)
                .boxed()
                .distinct()
                .collect(Collectors.toList());
        return collect;
    }

    //hashset去重
    public static Set<Integer> removeDupicates3(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        return set;
    }
}
